package com.qingcloud.adminbackend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.source.DataSource;
import com.qingcloud.adminbackend.entity.source.DataSourceDatabaseDto;
import com.qingcloud.adminbackend.enums.DataSourceTypeEnum;
import com.qingcloud.adminbackend.mapper.DataSourceMapper;
import com.qingcloud.adminbackend.service.IDataSourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangjinnan
 * @since 2021-03-17
 */
@Service
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements IDataSourceService {

    @Resource
    private DataSourceMapper dataSourceMapper;

    public List<DataSource> findAll() {

        return dataSourceMapper.findAll();
    }

    @Override
    public List<DataSourceDatabaseDto> findAllDsDb() {
        return dataSourceMapper.findAllDsDb();
    }

    @Override
    public AjaxResult addDatasource(DataSource dataSource) {
        String check = DataSource.check(dataSource);
        if (!check.equalsIgnoreCase("true"))
            return new AjaxResult<>(WamingConstant.FAILURE, check, "");
        int addFlag = dataSourceMapper.addDatasource(dataSource);
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "写入数据库失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "增加成功", "");
    }

    @Override
    public AjaxResult deleteDatasourceByIds(String ids) {
        if (ids == null) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", "");
        String[] idArr = ids.split(",");
        if (idArr.length == 0) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", "");
        int addFlag = dataSourceMapper.deleteDatasourceByIds(Arrays.asList(idArr));
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "删除失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "删除成功", "");
    }

    @Override
    public AjaxResult updateDatasource(DataSource dataSource) {
        String check = DataSource.check(dataSource);
        if (!check.equalsIgnoreCase("true"))
            return new AjaxResult<>(WamingConstant.FAILURE, check, "");
        if (dataSource.getId() == null) return new AjaxResult<>(WamingConstant.FAILURE, "id参数不能为空", null);
        List<DataSource> dataSourceList = dataSourceMapper.findDatasourceByIds(new ArrayList<>(Collections.singletonList(dataSource.getId() + "")));
        if (dataSourceList.size() == 0) return new AjaxResult<>(WamingConstant.FAILURE, "无记录", "");
        int addFlag = dataSourceMapper.updateDatasource(dataSource);
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "修改失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "修改成功", "");
    }

    @Override
    public AjaxResult copyDatasourceById(String id) {
        if (id == null) return new AjaxResult<>(WamingConstant.FAILURE, "id参数不能为空", null);
        List<DataSource> dataSourceList = dataSourceMapper.findDatasourceByIds(new ArrayList<>(Collections.singleton(id)));
        if (dataSourceList.size() == 0) return new AjaxResult<>(WamingConstant.FAILURE, "无记录", "");
        DataSource dataSourceOld = dataSourceList.get(0);
        dataSourceOld.setName(dataSourceOld.getName() + "-复制");
        int addFlag = dataSourceMapper.addDatasource(dataSourceOld);
        if (addFlag <= 0) return new AjaxResult<>(WamingConstant.FAILURE, "写入数据库失败", "");
        else return new AjaxResult<>(WamingConstant.SUCCESS, "增加成功", "");
    }

    @Override
    public AjaxResult<List<DataSource>> findDatasourceByIds(String ids) {
        if (ids == null) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", null);
        String[] idArr = ids.split(",");
        if (idArr.length == 0) return new AjaxResult<>(WamingConstant.FAILURE, "ids参数不能为空", null);
        return new AjaxResult<>(WamingConstant.SUCCESS, "", dataSourceMapper.findDatasourceByIds(Arrays.asList(idArr)));
    }

    @Override
    public AjaxResult<List<DataSource>> findDatasourceByName(String name, String projectId) {
        return new AjaxResult<>(WamingConstant.SUCCESS, "", dataSourceMapper.findDatasourceByName(name, projectId));
    }

    @Override
    public AjaxResult<List<DataSource>> findDatasourceByProjectId(String projectId) {
        return new AjaxResult<>(WamingConstant.SUCCESS, "", dataSourceMapper.findDatasourceByProjectId(projectId));
    }

    @Override
    public AjaxResult<List<Map<String, Object>>> getTypes() {

        List<Map<String, Object>> result = new ArrayList<>();
        Arrays.asList(DataSourceTypeEnum.values()).forEach(it -> {
            Map<String, Object> map = new HashMap<>();
            map.put("code", it.code);
            map.put("title", it.title);
            result.add(map);
        });
        return new AjaxResult<>(WamingConstant.SUCCESS, "", result);
    }
}
